package com.kunbo.cn.service.impl.core.card;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.card.IssuanceCardDao;
import com.kunbo.cn.entity.core.card.IssuanceCard;
import com.kunbo.cn.service.core.card.IssuanceCardService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.Pages;

@Service
public class IssuanceCardServiceImpl  implements IssuanceCardService{
	@Autowired
	private IssuanceCardDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	

	@Override
	public void save(IssuanceCard card) {
		card.setMake_time(SDate.getSystemDateToString());
		card.setState("正常");
		dao.save(card);
	}
	@Override
	public void update(IssuanceCard card) {
		
		if( null!=card) {
			dao.save(card);
		}
		System.out.println("打印：+++="+card.toString());
		
	}//PatchCard
	@Override
	public void PatchCard(IssuanceCard card) {
		dao.save(card);
		
	}
	@Override
	public int updateState(String id, String state) {
		String sql = "UPDATE \r\n" +
		        "business_issuance_card_manage  \r\n"+
		        " set state="+state+"\r\n "+
		        " WHERE  id='"+id+"'";
				 int i=jdbc.update(sql);
				return i;
	}

	@Override
	public void delete(Integer id) {
		dao.delete(id);
	}

	@Override
	public IssuanceCard findById(Integer id) {
		return dao.findOne(id);
	}

	@Override
	public Pages list(String ic_card_number,String car_number,String id_card,String state,Integer page,Integer pageSize) {
			String sql1 = "select * from business_issuance_card_manage where 1=1 ";
	
	if (id_card != null && !id_card.equals("")) {
		sql1 += " and id_card like'%" + id_card +"%'" ;
	}
	
	if (ic_card_number != null && !ic_card_number.equals("")) {
		sql1 += " and ic_card_number ='" + ic_card_number +"'" ;
	}if (car_number != null && !car_number.equals("")) {
		sql1 += " and car_number  like'%" + car_number +"%'" ;
	}if (state != null && !state.equals("")) {
		sql1 += " and state ='" + state +"'" ;
}
	String sql2="";
	sql2+= " order by id  desc  limit " + (page * pageSize) +"," + pageSize;
	System.out.println(sql1+"<!!!的!!!!!!!!!!!!!!!!!");
	  String sql=sql1.concat(sql2);//获取数据sql
	List<Map<String, Object>> list = jdbc.queryForList(sql);
	Pages pages =new Pages();
	  Integer count = jdbc.queryForList(sql1).size();
	  pages.setTotal(count);
	  pages.setData(list);
	return pages;
}
	@Override
	public List<Map<String, Object>> carDriver() {
		// TODO Auto-generated method stub
		String sql = "select  contact_number,id , name ,idcrad from driver_management where 1=1";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> carNumber() {
		String sql = "select id,license_plate_number  from  vehicle_management where 1=1 ";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> findByIdCard(String idCard) {
		String sql = "select * from business_issuance_card_manage where id_card = '"+idCard+"'";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		return list;
	}
	@Override
	public List<Map<String, Object>> findByIdCardNumber(String icCardNumber) {
		String sql = "select * from business_issuance_card_manage where ic_card_number = '"+icCardNumber+"'";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		return list;
	}
	@Override
	public int logOffIC(String icNumber) {
		String sql = "delete from business_issuance_card_manage where ic_card_number = '"+icNumber+"'"; 
		int r = jdbc.update(sql);
		return r;
	}
	
}
